package com.rabincorp.tactile.task;

import com.rabincorp.tactile.common.TactileCommon;
import com.rabincorp.tactile.entity.YouTubeVideoItem;
import com.rabincorp.tactile.feed.YouTubeFeed;
import com.rabincorp.tactile.util.CacheService;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author irabin
 */
public class FeedUpdateCronJob {

    private static final Logger logger = Logger.getLogger(FeedUpdateCronJob.class);
    private static FeedUpdateCronJob instance = null;

    public static FeedUpdateCronJob getInstance() {
	if (instance == null) {
	    synchronized (FeedUpdateCronJob.class) {
		instance = new FeedUpdateCronJob();
	    }
	}
	return instance;
    }

    private FeedUpdateCronJob() {
    }

    public void refreshFeed() {
	logger.info("Starting FeedUpdateCronJob...");

	long start = System.currentTimeMillis();
        YouTubeFeed youtubeService = YouTubeFeed.getInstance();
        List<YouTubeVideoItem> videoList = youtubeService.parse(TactileCommon.GAGS_CHANNEL);
        CacheService.getDefault().putCachedObject(videoList, TactileCommon.CACHE_SERVICE_NAME, TactileCommon.CACHE_YOUTUBE_ID);
        
	long end = System.currentTimeMillis();
	
	logger.info("Successfully finished FeedUpdateCronJob in " + (end - start) + " ms!");

    }
}
